package org.batatasframework.support.searchable.result;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.batatasframework.support.database.spi.ResultWritable;


public class GroupResultRow implements ResultRow{
	private final List<SearchResultRow> subrows = new ArrayList<>();
	private final List<SearchResultHead> heads;
	
	private final Object value;
	
	public GroupResultRow(List<SearchResultHead> heads, Object value) {
		this.heads = heads;
		this.value = value;
	}

	@Override
	public void read(ResultSet rs) throws Exception {
		SearchResultRow row = new SearchResultRow(heads);
		row.read(rs);
		subrows.add(row);
	}

	@Override
	public void writeTo(ResultWritable resultWriter) throws Exception {
		resultWriter.group(value, subrows.size());
		for(SearchResultRow row : subrows){
			row.writeTo(resultWriter);
		}
	}

}
